package com.sendi.system.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONObject;
import org.hibernate.Query;
import org.hibernate.Session;
 
import com.sendi.system.entity.User;
public class SysLogAction extends SysAction {
    private Integer  id;
 
	
	private String logname;
	private String loguser;
	private String logterminal;
	private String logresult;
	private String logcontent;
	private String logtime;
	
 
	public SysLogAction()
	{
		super();
		data_session_name="SysLogActionListData";
	}
	
	
	public void sysLogInit()
	{
		List datas=null;
		
		if(getSession().getAttribute(data_session_name)!=null)
		{
			datas=(List)getSession().getAttribute(data_session_name);
			System.out.println("use session size"+datas.size());
		}
		else
		{
			addsyslogTosessin();
			datas=(List)getSession().getAttribute(data_session_name);
		}
		 
			writeJSONArray(datas);
		 
	}
	
	public void addsyslogTosessin()
	{
		List datas=null;
		String hql="from  Syslogs";
	 	datas=serviceSupportImp.findByHQL(hql);
	  	getSession().setAttribute(data_session_name,datas);
	 	System.out.println("data size:"+datas.size());
	}
	
 
   /*
    * 查询用户
    */
	public void querySyslog()
	{
		System.out.println("---------query"+logname);
		   try
		   {
			     Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
			 	StringBuffer hql=new StringBuffer("from Syslogs where 1=1 ");
				if(loguser!=null&&loguser.trim().length()>0)
				{
					hql.append(" and  loguser like :loguser ");
				}
				if(logname!=null&&logname.trim().length()>0)
				{
					hql.append(" and logname like :logname");
				}
				if(logterminal!=null&&logterminal.trim().length()>0)
				{
					hql.append(" and logterminal like :logterminal");
				}
				if(logcontent!=null&&logcontent.trim().length()>0)
				{
					hql.append(" and logcontent like :logcontent");
				}
				
		 		   Query query= session.createQuery(hql.toString());
				   
				   if(loguser!=null&&loguser.trim().length()>0)
					{
					   query.setParameter("loguser", "%"+loguser+"%");
					}
					if(logname!=null&&logname.trim().length()>0)
					{
						  query.setParameter("logname", "%"+logname+"%");
					}
					if(logterminal!=null&&logterminal.trim().length()>0)
					{
						  query.setParameter("logterminal", "%"+logterminal+"%");
					}
					if(logcontent!=null&&logcontent.trim().length()>0)
					{
						  query.setParameter("logcontent", "%"+logcontent+"%");
					}
					
				   
					     List datas=query.list();
					    System.out.println("query size"+datas.size());
				     	getSession().setAttribute(data_session_name,datas);	
				     	writeResponseText("{success:true,msg:'success',datasize:"+datas.size()+"}");
		   			}
		   catch (Exception e) {
			   writeResponseText("{success:false,msg:'failure'}");
				 
		}
		    
		
	}

  		public void excuteDeleteSyslogs()
		{
			 
			 String hql="delete Syslogs where id in ("+deleterecord+")";
			 
		  Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
		  session.beginTransaction();
		  session.createQuery(hql).executeUpdate();
		  session.getTransaction().commit();
		  addsyslogTosessin();
		  writeResponseText("{success:true,msg:'success'}"); 
		}
	public Integer getId() {
		return id;
	}



	public void setId(Integer id) {
		this.id = id;
	}


	public String getLogname() {
		return logname;
	}


	public void setLogname(String logname) {
		this.logname = logname;
	}


	public String getLoguser() {
		return loguser;
	}


	public void setLoguser(String loguser) {
		this.loguser = loguser;
	}


	public String getLogterminal() {
		return logterminal;
	}


	public void setLogterminal(String logterminal) {
		this.logterminal = logterminal;
	}


	public String getLogresult() {
		return logresult;
	}


	public void setLogresult(String logresult) {
		this.logresult = logresult;
	}


	public String getLogcontent() {
		return logcontent;
	}


	public void setLogcontent(String logcontent) {
		this.logcontent = logcontent;
	}


	public String getLogtime() {
		return logtime;
	}


	public void setLogtime(String logtime) {
		this.logtime = logtime;
	}



  
	

}
